Option Strict On

Imports Microsoft.Office.Interop
Imports Microsoft.Office.Interop.Access

Imports System.Xml
Imports System.IO
Imports System.Xml.Serialization
Imports System.Runtime.InteropServices

Public Enum enumDebugLevel
    FULL
    SUMMARY
End Enum

''' <summary>
''' Creates an access database from SVN sources
''' </summary>
''' <remarks></remarks>
Public Class CreateAccessDBFromSVN
    Inherits AccessSvnTransformer

    Public Overrides Sub Transform()
        Call raiseTransformStarted()
        Try

            Dim accFilename As String = Me.getFinalAccessFilename
            Me.LoadObjectList()
            Me.openDatabase()
            ' Call Me.app.CurrentProject.OpenConnection("Integrated Security=SSPI;Persist Security Info=False;User ID=payroll;Initial Catalog=prl_unit_testCHRIS_PC;Data Source=chris-pc\sqlexpress")

            Me.TransformObjects()

            Try
                Me.app.RunCommand(AcCommand.acCmdCompileAndSaveAllModules)
            Catch e As Exception
                Console.WriteLine("Compilation of database failed.  Please compile manually")
            End Try

            Me.app.CloseCurrentDatabase()


            Console.WriteLine("will compact to database file: " & accFilename)

            Dim compacted As Boolean = Me.app.CompactRepair(Me.getTmpAccessFileName, _
                                      accFilename, True)

            Call IO.File.Delete(Me.getTmpAccessFileName)

            Console.WriteLine("Created database file: " & accFilename)

        Catch e As Exception
            Me.raiseTransformError(e.Message)
        Finally

            If Me.app IsNot Nothing Then

                Me.app.Quit()

                GC.Collect()
                GC.WaitForPendingFinalizers()

                If Me.app IsNot Nothing Then Marshal.ReleaseComObject(Me.app)
                Me.app = Nothing

            End If
            Call raiseTransformEnd()

        End Try


    End Sub


    Protected Sub TransformObjects()

        For Each ex In Me.Transformers

            ex.App = app
            ex.BaseExportPath = AppContext.current.RootSVNPath
            ex.debugLevel = Me.debugLevel
            ex.ObjectList = Me.ObjectList
            ex.Load()

        Next
    End Sub
    Protected Overridable Sub openDatabase()
        'copy database from \DB folder to the path below svn root

        Dim dbFilename As String = getTmpAccessFileName()
        If File.Exists(dbFilename) Then
            'Throw New ApplicationException("Access working File already exists:" & lworkingFile)
            IO.File.Delete(dbFilename)
        End If

        File.Copy(AppContext.current.RootSVNPath & "\DB\dbfile", dbFilename)

        Me.app = New Access.Application
        Me.app.Visible = False
        Me.app.UserControl = False

        Console.WriteLine("Access Application version:" & Me.app.Version & ", build " & Me.app.Build)

        If dbFilename.ToLower.EndsWith("adp") Then
            Me.app.OpenAccessProject(dbFilename, False)
        Else
            Me.app.OpenCurrentDatabase(dbFilename, False)
        End If

        Dim snvRev As String = CStr(Me.SvnClient.getSVNCurrentRevision)
        Dim snvRepos As String = CStr(Me.SvnClient.getSVNRepository)

        AppContext.current.AddOrChangeAccessProperty(Me.app, "svnRevision", snvRev)
        AppContext.current.AddOrChangeAccessProperty(Me.app, "svnRepository", snvRepos)

        Console.WriteLine("Created tmp database file: " & dbFilename & ", access file version:" & Me.app.DBEngine.Version)
        Console.WriteLine("SVN Revision:" & snvRev)
        Console.WriteLine("SVN URL:" & snvRepos)

    End Sub

    Public Function getFinalAccessFilename() As String
        Dim tmpFilename As String = AppContext.current.getAccessDatabaseFullName
        Dim f As FileInfo = New FileInfo(tmpFilename)
        Return f.FullName
    End Function

    Private Function getTmpAccessFileName() As String

        Dim tmpFilename As String = AppContext.current.getAccessDatabaseFullName & ".tmp"
        Dim f As FileInfo = New FileInfo(tmpFilename)
        Return f.FullName

    End Function

    Protected Overridable Sub LoadObjectList()

        Dim myXmlDocument As XmlDocument = New XmlDocument()
        myXmlDocument.Load(AppContext.current.RootSVNPath & "\ObjectList.xml")

        Dim cnodes As XmlNodeList = myXmlDocument.GetElementsByTagName("DatabaseObject")

        For Each node As XmlNode In cnodes

            Dim oname As String = node.ChildNodes(0).InnerText
            Dim otype As Integer = CInt(node.ChildNodes(1).InnerText)

            Me.ObjectList.Add(New ExportedAccessObject(oname, CType(otype, SVNAccessObjectType)))

        Next
    End Sub

End Class
